<% include header.ejs %>
<nav id="post">
    <div>
        <a href="/" >首页</a>
        <span ><%= title %></span>
    </div>
</nav>
<p style="height: 44px"></p>
<div  ng-controller="updatePostCtrl" ng-app="modifyApp">
      <section class="col-sm-12" >
        <form class="form-horizontal" name="updatePostForm" ng-submit="updatePost(updatePostForm.$valid)" novalidate>
            <h3>
            <small class="text-danger hide" id="errorInfo">提交内容出现错误</small>
            </h3>
            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-1">
                    <label class="control-label">选择要发布的类型：</label>
                    <select class="form-control" ng-model="upFormData.selectName" ng-options="ty.id as ty.name for ty in names" required></select>
                </div>
            </div>
            <!-- 名称 -->
            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-1">
                    <input type="text" ng-cloak class="form-control" name="title" id="title"  ng-maxlength="10" ng-model="upFormData.title" placeholder="名称">
                    <label ng-cloak for="inputError" class="control-label text-danger" ng-show="updatePostForm.title.$invalid && !updatePostForm.title.$pristine"><i class="glyphicon glyphicon-exclamation-sign"></i>名称不得超过十个字</label>
                </div>
            </div>
            <!-- 产地 -->
            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-1">
                    <input type="text" ng-cloak name="city" class="form-control"  ng-model="upFormData.city" placeholder="产地">
                </div>
            </div>
            <!-- 单价 -->
            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-1">
                    <input type="text" ng-cloak name="price" class="form-control"  ng-model="upFormData.price" placeholder="单价">
                </div>
            </div>
            <!-- 联系电话 -->
            <div class="form-group">
                <div class="col-sm-6 col-sm-offset-1">
                    <input type="text" ng-cloak name="phone" class="form-control" ng-maxlength="11" ng-model="upFormData.phone" placeholder="联系电话">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-1 col-sm-6 ">
                    <button class="btn btn-success ">提交</button>
                </div>
            </div>
        </form>
    </section>

</div>
<% include footer.ejs %>
<script type="text/javascript">
 var modifyApp = angular.module('modifyApp', []);
 modifyApp.controller('updatePostCtrl', function ($scope, $http) {
    $scope.names = 
    [{id:'fruit',name:'水果蔬菜'},
    {id:'grain',name:'粮油米面'},
    {id:'tree',name:'苗木花卉'},
    {id:'water',name:'畜牧水产'},
    {id:'machine',name:'农资农机'}];
    $scope.upFormData = {}; 
    $scope.upFormData.author = '<%= userInfo.userName %>';
         //查询文章的信息
         $http.get('/updatePost?_id=<%= postId %>')
         .success(function(result){
            $scope.upFormData = result;
        })
        //查询用户的信息
        $http.get('/userInfo?_id=' + '<%= userInfoId %>')
        .success(function(result){
            $scope.rightData = result;
        });

        $scope.updatePost = function (isVaild) {
            if ( isVaild ) {
                //如果表单内容格式正确
                $http({
                    method: 'POST',
                    url: '/updatePost',
                    data: $.param($scope.upFormData),
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                }).success(function (data) {
                    if (data === 'success') {
                        alert('农产品信息修改成功！');
                        window.location.href = '/backStage';
                    } else {
                        $('#errorInfo').removeClass('hide').text(data);
                    }
                })
            } else {
                alert('输入有误！');
            }
        }
    })
</script>